package com.kh.tool.exceldatachecker;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.util.StringUtils;

import java.util.List;

public abstract class AbstractTableHeaderChecker implements TableHeaderChecker {
    @Override
    public boolean check(Sheet sheet) {
        Row headerRow = sheet.getRow(0);
        if (headerRow == null) {
            return false;
        }

        List<ExcelColumns> tableHeader = createTableHeader();
        short lastCellNum = headerRow.getLastCellNum();
        short firstCellNum = headerRow.getFirstCellNum();
        if(lastCellNum != tableHeader.size()) {
            return false;
        }
        for (int i = firstCellNum; i < lastCellNum; i++) {
            String cellValue = headerRow.getCell(i).getStringCellValue();
            if (StringUtils.isEmpty(cellValue)) {
                return false;
            }
            if (!cellValue.trim().equals(tableHeader.get(i).getValue())) {
                return false;
            }
        }
        return true;
    }

    public abstract List<ExcelColumns> createTableHeader();
}
